package battle
{
	import data.PlayerInfo;
	import data.SoldierInfo;
	
	import events.ItemEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	
	import map.MapInfo;

	public class GameInfo extends EventDispatcher
	{
		private var _turn:int;
		private var _mapInfo:MapInfo;
		public function GameInfo()
		{
		}
		
		public function get turn():int
		{
			return _turn;
		}
		
		private var _players:Dictionary = new Dictionary(true);
		private var _soldiers:Dictionary = new Dictionary(true);
		
		public function addPlayer(player:PlayerInfo):void
		{
			_players[player.id] = player;
			for each(var info:SoldierInfo in player.soldiers)
			{
				var soldier:Soldier = new Soldier(info);
				_soldiers[soldier.info.id] = soldier;
			}
			player.addEventListener(ItemEvent.ADD_ITEM,onSoldierAdd);
			player.addEventListener(ItemEvent.REMOVE_ITEM,onSoldierRemove);
		}
		
		public function removePlayer(player:PlayerInfo):void
		{
			player.removeEventListener(ItemEvent.ADD_ITEM,onSoldierAdd);
			player.removeEventListener(ItemEvent.REMOVE_ITEM,onSoldierRemove);
			delete _players[player.id];
		}
		
		private function onSoldierRemove(event:ItemEvent):void
		{
			delete _soldiers[event.item.id];
		}
		
		private function onSoldierAdd(event:ItemEvent):void
		{
			var soldier:Soldier = new Soldier(event.item);
			_soldiers[soldier.info.id] = soldier;
		}
		
		public function get soldiers():Dictionary
		{
			return _soldiers;
		}
		
		public function getPlayerByID(id:int):PlayerInfo
		{
			return _players[id];
		}
		
		public function set mapInfo(info:MapInfo):void
		{
			_mapInfo = info;
		}
		
		public function get mapInfo():MapInfo
		{
			return _mapInfo;
		}
		
		private var _currentPlayer:PlayerInfo;
		
		public function get currentPlayer():PlayerInfo
		{
			return _currentPlayer;
		}
		
		public function beginNewTurn(player:PlayerInfo):void
		{
			_currentPlayer = player;
			_turn++;
			dispatchEvent(new Event(Event.CHANGE));
		}
	}
}